package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.Order;
import com.itheima.reggie.domain.OrderDetail;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    //下单列表
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    //订单明细
    @Autowired
    private OrderMapper orderMapper;
    //获取购物车信息
    @Autowired
    private CartService cartService;
    //获取地址信息
    @Autowired
    private AddressService addressService;

    //下单功能
    @Override
    public void save(Order order) {

        Address addressDefault = addressService.findDefault();
        //让程序变的更加健壮 加一些判断
        if (addressDefault == null) {
            throw new CustomException("地址信息异常 下单失败");
        }
        //先生成订单id
        Long orderId = IdUtil.createSnowflake(1, 1).nextId();

        //获得购物车信息
        List<Cart> cartList = cartService.list();
        //判断购物车是否为空
        if (CollUtil.isEmpty(cartList)) {
            throw new CustomException("购物车异常 下单失败");
        }
        //设置订单所有金额参数
        BigDecimal amount = new BigDecimal("0");
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            //取出购物车信息  存入订单明细里面
            orderDetail.setName(cart.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount());

            orderDetailMapper.insert(orderDetail);
            //计算购物车算出订单金额
            amount = amount.add(new BigDecimal(String.valueOf(orderDetail.getAmount())).multiply(new BigDecimal(orderDetail.getNumber())));
        }
        //补全订单信息
        order.setId(orderId);
        order.setNumber(orderId.toString());
        order.setStatus(1);

        order.setUserId(UserHolder.get().getId());
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(amount);
        //获取地址信息

        order.setPhone(addressDefault.getPhone());
        order.setAddress(addressDefault.getDetail());
        order.setConsignee(addressDefault.getConsignee());
        //保存订单 并且清除购物车
        orderMapper.insert(order);
        cartService.clear();
    }

    @Override
    public Page<Order> orderPage(Integer page, Integer pageSize, Long number, Date beginTime, Date endTime) {
        //构建条件
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        //可传可不传的信息 要加非空判断
        qw.between(beginTime != null & endTime != null, Order::getOrderTime, beginTime, endTime);

        qw.eq(number != null, Order::getId, number);
        qw.orderByDesc(Order::getOrderTime);
        //构建分页查询
        Page<Order> orderPage = new Page<>(page, pageSize);
        orderPage = orderMapper.selectPage(orderPage, qw);

        List<Order> orderList = orderPage.getRecords();
        //把订单明细换到订单里
        for (Order order : orderList) {

            LambdaQueryWrapper<OrderDetail> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(qw1);
            order.setOrderDetails(orderDetails);
        }


        return orderPage;
    }
}
